package org.example.order.controller;

import org.example.order.entity.CartItem;
import org.example.order.entity.Order;
import org.example.order.service.CartService;
import org.example.order.common.APIResponse;
import org.example.order.common.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @PostMapping("/addToCart")
    public APIResponse addToCart(@RequestParam Long userId, @RequestParam Long productId, @RequestParam Integer quantity) {
        cartService.addToCart(userId, productId, quantity);
        return new APIResponse(ResponseCode.SUCCESS, "添加到购物车成功");
    }

    @PostMapping("/updateQuantity")
    public APIResponse updateQuantity(@RequestParam Long userId, @RequestParam Long productId, @RequestParam Integer quantity) {
        cartService.updateQuantity(userId, productId, quantity);
        return new APIResponse(ResponseCode.SUCCESS, "更新数量成功");
    }

    @DeleteMapping("/removeFromCart")
    public APIResponse removeFromCart(@RequestParam Long userId, @RequestParam Long productId) {
        cartService.removeFromCart(userId, productId);
        return new APIResponse(ResponseCode.SUCCESS, "从购物车移除成功");
    }

    @DeleteMapping("/clearCart")
    public APIResponse clearCart(@RequestParam Long userId) {
        cartService.clearCart(userId);
        return new APIResponse(ResponseCode.SUCCESS, "从购物车清除成功");
    }

    @GetMapping("/getCartList")
    public APIResponse<List<CartItem>> getCartList(@RequestParam Long userId) {
        List<CartItem> cartList = cartService.getCartList(userId);
        return new APIResponse<>(ResponseCode.SUCCESS, "获取购物车列表成功", cartList);
    }

    @PostMapping("/submitOrder")
    public APIResponse<Order> submitOrder(@RequestParam Long userId, @RequestParam String receiverAddress, @RequestParam String receiverName, @RequestParam String receiverPhone, @RequestParam String remark) {
        Order order = cartService.submitOrder(userId, receiverAddress, receiverName, receiverPhone, remark);
        return new APIResponse<>(ResponseCode.SUCCESS, "提交订单成功", order);
    }
}